package com.gs.SynAntiWandering.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.apiresult.PagedList;
import com.gs.SynAntiWandering.db.dto.ShortDto;
import com.gs.SynAntiWandering.db.dto.MemberDto;
import com.gs.SynAntiWandering.db.query.BaseQuery;
import com.gs.SynAntiWandering.model.ComboModel;
import com.gs.SynAntiWandering.model.listener.OnResultListener;
import com.gs.SynAntiWandering.model.MemberModel;
import com.gs.SynAntiWandering.view.IMemberView;

import java.io.IOException;
import java.util.List;

public class MemberPresenter extends BasePresenter<IMemberView> {
    private MemberModel model = new MemberModel();
    private ComboModel comboModel = new ComboModel();
    public void getData(BaseQuery query) {
        model.getData(query, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetDataSuccess((ApiResultData<PagedList<MemberDto>>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetDataFails(e);
                }
            }
        }, new TypeToken<ApiResultData<PagedList<MemberDto>>>() {
        }.getType());
    }

    public void combo(String typeKey) {
        comboModel.combo(typeKey, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetComboSuccess((ApiResultData<List<ShortDto>>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetComboFails(e);
                }
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void delete(String id, int position) throws IOException {
        getView().showLoading();
        model.delete(id, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onDeleteMemberSuccess((ApiResultData<String>) aipResult,position);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onDeleteMemberFails(e);
                }
            }
        },new TypeToken<ApiResultData<String>>(){}.getType());
    }
}
